package com.netease.homework.web.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.netease.homework.meta.Selled;
import com.netease.homework.service.ISelledService;

/**
 * 
 * @author smart
 * @时间 2019年3月8日
 * @描述 售出接口
 */
@Controller
@RequestMapping("/selled")
public class SelledCtrl {

	@Autowired
	@Qualifier("SelledService")
	private ISelledService selledService;
	
	@InitBinder 
	public void initBinder(WebDataBinder binder) { 
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		dateFormat.setLenient(true); 
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 
	}

	
	@RequestMapping(value = "/getSelledById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSelledById(HttpServletRequest request,Integer id) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = selledService.getSelledById(id);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
	
	
	
	@RequestMapping(value = "/addSelled", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addSelled(HttpServletRequest request,Integer userId,Integer commodityId,Date time,Integer number,Double price) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = selledService.addSelled(userId,commodityId,time,number,price);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
	
	@RequestMapping(value = "/addSelledByName", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addSelledByName(HttpServletRequest request,String userName,Integer commodityId,Integer number,Double price) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = selledService.addSelledByName(userName,commodityId,number,price);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
	@RequestMapping(value = "/addSelledListByName", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addSelledListByName(@RequestBody ArrayList<Selled> list) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = selledService.addSelledListByName(list);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
	
	@RequestMapping(value = "/getSelledByUserName", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSelledByUserName(HttpServletRequest request,String userName) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = selledService.getSelledByUserName(userName);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
	@RequestMapping(value = "/getSelledByCommId", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getSelledByCommId(HttpServletRequest request,Integer id) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = selledService.getSelledByCommId(id);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}
}
